package com.brid.aop.aspectj;

import com.brid.aop.anno.Logger;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author: sijunqiang
 * @Date: 2020/4/20 18:00
 * @Description:
 */
@Component
@Slf4j
@Aspect
public class LogAspect {


    @Pointcut("@annotation(com.brid.aop.anno.Logger)")
    public void logMethod() {
    }


    /**
     * 定义一个环绕通知方法
     *
     * @param joinPoint
     * @return
     */
    @Around("logMethod()") //切入点
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        log.info("环绕通知:  signature:{}", signature);

        Method method = signature.getMethod();
        log.info("method:{}", method);

        Class<?> targetClass = method.getDeclaringClass();
        log.info("target:{}", targetClass);

        StringBuilder classMethod = new StringBuilder();
        Logger annotation = method.getAnnotation(Logger.class);
        if (annotation != null) {
            classMethod.append(annotation.value());
        }
        String target = targetClass.getName().concat("#").concat(method.getName());

        Object[] args = joinPoint.getArgs();
        System.out.println("args = " + args);


        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        Object proceed = joinPoint.proceed();
        log.info("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

         return proceed;
    }


}
